(function() {

	var appModule = angular.module('tracecashModule');

	appModule.directive('observationList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/observation/observation-list.html',
			require : '^ngModel',
			controller : 'ObservationListCtrl',
			scope : {
				observations : '=ngModel',
				sinisterId : '=sinisterId',
				config : '='
			}
		};
	});

	appModule.controller('ObservationListCtrl', ['$scope', '$rootScope', '$routeParams', '$http', '$modal', '$translate', function($scope, $rootScope, $routeParams, $http, $modal, $translate) {
		$scope.remove = function(observation) {
			$translate('confirmDialog').then(function(text) {
				if (window.confirm(text)) {
					$http({
						method : 'DELETE',
						url : $rootScope.REST_PATH + '/observation/delete/' + observation.id
					}).success(function(data) {
						$scope.message = data;
						$scope.observations = data.payload;
					});
				}
			});
		};
		$scope.add = function() {
			$scope.open({});
		};
		$scope.edit = function(observation) {
			$scope.open(observation);
		};
		$scope.open = function(observation) {
			var modalInstance = $modal.open({
				templateUrl : 'html/observation/observation-detail.html',
				controller : 'ObservationDetailCtrl',
				size : 'lg',
				resolve : {
					sinisterId : function() {
						return $scope.sinisterId;
					},
					observation : function() {
						return observation;
					}
				}
			});
			modalInstance.result.then(function(data) {
				$scope.message = data;
				$scope.observations = data.payload;
			}, function() {
			});
		};
	}]);

	appModule.controller('ObservationDetailCtrl', ['$scope', '$rootScope', '$http', '$modalInstance', 'sinisterId', 'observation', function($scope, $rootScope, $http, $modalInstance, sinisterId, observation) {
		$scope.observation = observation;
		$scope.save = function() {
			$http.post($rootScope.REST_PATH + '/observation/save/' + sinisterId, $scope.observation).success(function(data) {
				if (data.code == $rootScope.CODE_SUCCESS) {
					$modalInstance.close(data);
				} else {
					$scope.message = data;
				}
			});
		};
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel');
		};
	}]);

})();